package com.zqx.flyweight.factory;

import com.zqx.flyweight.flyweight.BlackIgoChessman;
import com.zqx.flyweight.flyweight.IgoChessman;
import com.zqx.flyweight.flyweight.WhiteIgoChessman;

import java.util.Hashtable;

/**
 * description: 享元工厂类
 * @author zqx
 * @date 2018/09/04
 */
public class IgoChessmanFactory {

    private static IgoChessmanFactory instance = new IgoChessmanFactory();
    private static Hashtable ht;//使用Hashtable来存储享元对象，充当享元池

    private IgoChessmanFactory() {
       ht = new Hashtable<>();
       IgoChessman black = new BlackIgoChessman();
       ht.put("black",black);
       IgoChessman white = new WhiteIgoChessman();
       ht.put("white",white);
    }

    //返回享元工厂类的唯一实例
    public static IgoChessmanFactory getInstance()
    {
        return instance;
    }

    //通过key获取存储在Hashtable中的享元对象
    public static IgoChessman getIgoChessman(String color)
    {
        return (IgoChessman) ht.get(color);
    }
}

